package com.wm.singleton;

import java.util.concurrent.atomic.AtomicReference;

/**
 * @ClassName: Singleton_06
 * @Description: CAS「AtomicReference」线程安全
 * @Author: WM
 * @Date: 2023/1/31 16:30
 */
public class Singleton_06 {
    private static final AtomicReference<Singleton_06> INSTANCE = new AtomicReference<Singleton_06>();

    private Singleton_06() {

    }

    public static final Singleton_06 getInstance() {
        for (; ; ) {
            Singleton_06 instance = INSTANCE.get();
            if (instance != null) {
                return instance;
            }
            INSTANCE.compareAndSet(null, new Singleton_06());
            return INSTANCE.get();
        }
    }

    public static void main(String[] args) {
        // com.wm.singleton.Singleton_06@45ee12a7
        System.out.println(Singleton_06.getInstance());
        // com.wm.singleton.Singleton_06@45ee12a7
        System.out.println(Singleton_06.getInstance());
    }
}
